/*
 * Copyright (c) 2007 Jonathan Bradshaw
 * 
 * This software is provided 'as-is', without any express or implied warranty. 
 * In no event will the authors be held liable for any damages arising from the use of this software.
 * Permission is granted to anyone to use this software for any purpose, including commercial 
 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
 * 
 * 1. The origin of this software must not be misrepresented; you must not claim that you wrote 
 *    the original software. If you use this software in a product, an acknowledgment in the 
 *    product documentation would be appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
 *    being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using VmcController.AddIn.Commands;
using Microsoft.MediaCenter;

namespace VmcController.AddIn {
	public interface ICommand {
		string ShowSyntax();
		OpResult Execute(string param);
	}

	/// <summary>
	/// Manages the list of available remote commands
	/// </summary>
	public class RemoteCommands {
		private Dictionary<string, ICommand> m_commands = new Dictionary<string, ICommand>();

		/// <summary>
		/// Initializes a new instance of the <see cref="RemoteCommands"/> class.
		/// </summary>
		public RemoteCommands() {
			ICommand cmd_to_add = null;

			m_commands.Add("=== Input Commands: ==========", null);
			try {
				cmd_to_add = new SendKeyCmd('R', false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}

			m_commands.Add("button-rec", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x25);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}

			m_commands.Add("button-left", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x26);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-up", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x27);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-right", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x28);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-down", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x0d);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-ok", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x08);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-back", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('D', false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-info", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0xbb, false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-ch-plus", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0xbd, false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-ch-minus", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('M', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-dvdmenu", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('A', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-dvdaudio", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('U', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-dvdsubtitle", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('C', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-cc", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('P', false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-pause", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('P', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-play", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('S', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-stop", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('B', false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-skipback", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('F', false, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-skipfwd", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('B', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-rew", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('F', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-fwd", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('Z', true, true, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-zoom", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x60);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-0", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x61);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-1", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x62);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-2", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x63);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-3", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x64);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-4", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x65);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-5", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x66);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-6", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x67);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-7", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x68);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-8", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x69);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-9", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('3', true, false, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-star", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd('8', true, false, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-num-number", cmd_to_add);
			try {
				cmd_to_add = new SendKeyCmd(0x1b);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("button-clear", cmd_to_add);
			try {
				cmd_to_add = new SendStringCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("type", cmd_to_add);

			m_commands.Add("=== Misc Commands: ==========", null);
			try {
				cmd_to_add = new DvdRomCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("dvdrom", cmd_to_add);
			try {
				cmd_to_add = new MsgBoxCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("msgbox", cmd_to_add);
			try {
				cmd_to_add = new MsgBoxRichCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("msgboxrich", cmd_to_add);
			try {
				cmd_to_add = new NavigateToPage();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("goto", cmd_to_add);

			m_commands.Add("=== Media Experience Commands: ==========", null);
			try {
				cmd_to_add = new FullScreenCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("fullscreen", cmd_to_add);
			try {
				cmd_to_add = new MediaMetaDataCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("mediametadata", cmd_to_add);
			try {
				cmd_to_add = new PlayRateCmd(true);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("playrate", cmd_to_add);
			try {
				cmd_to_add = new PlayRateCmd(false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("playrate-get", cmd_to_add);
			try {
				cmd_to_add = new PlayRateCmd(false, true);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("playstate-get", cmd_to_add);
			try {
				cmd_to_add = new PositionCmd(true);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("position", cmd_to_add);
			try {
				cmd_to_add = new PositionCmd(false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("position-get", cmd_to_add);

			m_commands.Add("=== Environment Commands: ==========", null);
			try {
				cmd_to_add = new VersionInfoCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("version", cmd_to_add);
			try {
				cmd_to_add = new CapabilitiesCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("capabilities", cmd_to_add);
			try {
				cmd_to_add = new ChangerCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("changer-load", cmd_to_add);

			m_commands.Add("=== Audio Mixer (Volume) Commands: ==========", null);
			try {
				cmd_to_add = new Volume();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("volume", cmd_to_add);

			m_commands.Add("=== Music Library Commands: ==========", null);
			try {
				cmd_to_add = new musicCmd(musicCmd.LIST_ARTISTS);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-list-artists", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.LIST_ALBUMS);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-list-albums", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.LIST_SONGS);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-list-songs", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.LIST_DETAILS);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-list-details", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.PLAY);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-play", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.QUEUE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-queue", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.SERV_COVER);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-cover", cmd_to_add);
			try {
				cmd_to_add = new musicCmd(musicCmd.CLEAR_CACHE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("music-clear-cache", cmd_to_add);

			m_commands.Add("=== Audio Library Commands: ==========", null);
			try {
				cmd_to_add = new artistCmd(false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-artists", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, artistCmd.show_artists + artistCmd.show_songs, artistCmd.by_artist);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-artist-songs", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, artistCmd.show_artists + artistCmd.show_albums, artistCmd.by_artist);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-artist-albums", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, artistCmd.show_artists + artistCmd.show_albums);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-albums", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false,
																	 artistCmd.show_artists + artistCmd.show_albums + artistCmd.show_songs, artistCmd.by_album);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-album-songs", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, 0, artistCmd.by_all);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-all-custom", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, 0, artistCmd.by_artist);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-artist-custom", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, 0, artistCmd.by_album);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-album-custom", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(false, false, false, 0, artistCmd.by_track);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-song-custom", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(true, false, false, artistCmd.show_artists + artistCmd.show_songs, artistCmd.by_artist);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-audio-artist", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(true, false, false, artistCmd.show_artists + artistCmd.show_albums + artistCmd.show_songs,
																	 artistCmd.by_album);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-audio-album", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(true, false, false, artistCmd.show_artists + artistCmd.show_albums + artistCmd.show_songs,
																	 artistCmd.by_track);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-audio-song", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(true, true, false, artistCmd.show_artists + artistCmd.show_songs, artistCmd.by_artist);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("queueaudio-artist", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(true, true, false, artistCmd.show_artists + artistCmd.show_albums + artistCmd.show_songs,
																	 artistCmd.by_album);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("queueaudio-album", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(true, true, false, artistCmd.show_artists + artistCmd.show_albums + artistCmd.show_songs,
																	 artistCmd.by_track);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("queueaudio-song", cmd_to_add);
			try {
				cmd_to_add = new artistCmd(0);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("list-artists-basic", cmd_to_add);

			m_commands.Add("=== Photo Library Commands: ==========", null);
			try {
				cmd_to_add = new photoCmd(photoCmd.CLEAR_CACHE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("photo-clear-cache", cmd_to_add);
			try {
				cmd_to_add = new photoCmd(photoCmd.LIST_PHOTOS);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("photo-list", cmd_to_add);
			try {
				cmd_to_add = new UnavailableCmd("NYI - Not yet implemented");
			} //photoCmd(photoCmd.PLAY_PHOTOS); }
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("photo-play", cmd_to_add);
			try {
				cmd_to_add = new UnavailableCmd("NYI - Not yet implemented");
			} //photoCmd(photoCmd.QUEUE_PHOTOS); }
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("photo-queue", cmd_to_add);
			try {
				cmd_to_add = new photoCmd(photoCmd.SERV_PHOTO);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("photo-serv", cmd_to_add);
			try {
				cmd_to_add = new photoCmd(photoCmd.LIST_TAGS);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("photo-tag-list", cmd_to_add);

			m_commands.Add("=== Play Commands: ==========", null);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.Audio, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-audio", cmd_to_add);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.Audio, true);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("queueaudio", cmd_to_add);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.Video, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-video", cmd_to_add);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.Dvd, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-dvd", cmd_to_add);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.Radio, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-radio", cmd_to_add);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.TV, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-tv", cmd_to_add);
			try {
				cmd_to_add = new PlayMediaCmd(MediaType.Dvr, false);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("play-dvr", cmd_to_add);

			//m_commands.Add("=== Epg / Schedule Commands: ==========", null);
			//try{
			//  cmd_to_add = new ScheduleCmd();
			//}
			//catch (Exception e){
			//  cmd_to_add = new UnavailableCmd(e.Message);
			//}
			//m_commands.Add("schedule", cmd_to_add);
			//try{
			//  cmd_to_add = new EpgCmd(EpgResultsType.ChannelName);
			//}
			//catch (Exception e){
			//  cmd_to_add = new UnavailableCmd(e.Message);
			//}
			//m_commands.Add("epg-lineup", cmd_to_add);
			//try{
			//  cmd_to_add = new EpgCmd(EpgResultsType.OnNow);
			//}
			//catch (Exception e){
			//  cmd_to_add = new UnavailableCmd(e.Message);
			//}
			//m_commands.Add("epg-onnow", cmd_to_add);
			//try{
			//  cmd_to_add = new EpgCmd(EpgResultsType.Details);
			//}
			//catch (Exception e){
			//  cmd_to_add = new UnavailableCmd(e.Message);
			//}
			//m_commands.Add("epg-details", cmd_to_add);

			m_commands.Add("=== Window State Commands: ==========", null);
			try {
				cmd_to_add = new SysCommand(SysCommand.SC_CLOSE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("window-close", cmd_to_add);
			try {
				cmd_to_add = new SysCommand(SysCommand.SC_MINIMIZE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("window-minimize", cmd_to_add);
			try {
				cmd_to_add = new SysCommand(SysCommand.SC_MAXIMIZE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("window-maximize", cmd_to_add);
			try {
				cmd_to_add = new SysCommand(SysCommand.SC_RESTORE);
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("window-restore", cmd_to_add);

			m_commands.Add("=== Reporting Commands: ==========", null);
			try {
				cmd_to_add = new customCmd();
			}
			catch (Exception e) {
				cmd_to_add = new UnavailableCmd(e.Message);
			}
			m_commands.Add("format", cmd_to_add);
		}

		/// <summary>
		/// Returns a multi-line list of commands and syntax
		/// </summary>
		/// <returns>string</returns>
		public OpResult CommandList() {
			return CommandList(0);
		}

		public OpResult CommandList(int port) {
			OpResult opResult = new OpResult(OpStatusCode.Ok);
			if (port != 0) {
				opResult.AppendFormat("=== Ports: ==========");
				opResult.AppendFormat("TCP/IP Socket port: {0}", port);
				opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10));
			}
			opResult.AppendFormat("=== Connection Commands: ==========");
			opResult.AppendFormat("help - Shows this page");
			opResult.AppendFormat("exit - Closes the socket connection");
			foreach (KeyValuePair<string, ICommand> cmd in m_commands) {
				opResult.AppendFormat("{0} {1}", cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax());
			}
			return opResult;
		}

		public OpResult CommandListHTML(int port) {
			OpResult opResult = new OpResult(OpStatusCode.Ok);

			string page_start =
				"<html><head><script LANGUAGE='JavaScript'>" +
				"function toggle (o){ var all=o.childNodes;  if (all[0].childNodes[0].innerText == '+') open(o,true);  else open(o,false);}" +
				"function open (o, b) { var all=o.childNodes;  if (b) {all[0].childNodes[0].innerText='-';all[1].style.display='inline';}  else {all[0].childNodes[0].innerText='+';all[1].style.display='none';}}" +
				"function toggleAll (b){ var all=document.childNodes[0].childNodes[1].childNodes; for (var i=0; i<all.length; i++) {if(all[i].id=='section') open(all[i],b)};}" +
				"</script></head>" +
				"<body><a id='top'>Jump to</a>: <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a>, <a href='#bottom'>Bottom</a><hr><font size=+3><a id='commands'>Command List</a>:&nbsp;&nbsp;</font>[<a onclick='toggleAll(true);' >Open All</a>] | [<a onclick='toggleAll(false);' >Collapse All</a>]<hr>";
			string page_end =
				"</pre></span></div>" +
				"<br><hr><b><a id='examples'>Note: URLs must be correctly encoded</a></b><hr><br>" +
				"<b>Note - The following custom examples require that:</b><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 - Custom formats #115 and 112 are defined in the &quot;artist.template&quot; file<br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 - The &quot;artist.template&quot; file has been copied to the ehome directory (usually C:\\Windows\\ehome)<br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 - Vista Media Center has been restarted after #1 and #2<br>" +
				"<br><b>Working track browser using custom formats: (can be slow... but this works as an album browser)</b><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display complete artist list linked to albums: <a href='/list-all-custom%20-112'>http://hostname:40510/list-all-custom%20-112</a><br>" +
				"<br><b>Examples using artist filter: (warning can be very slow with large libraries)</b><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All artists: <a href='/list-artists'>http://hostname:40510/list-artists</a><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All artists: <a href='/list-artists'>http://hostname:40510/list-artists</a><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All albums by artists starting with the letter &quot;A&quot;: <a href='/list-albums%20a'>http://hostname:40510/list-albums%20a</a><br>" +
				"<br><b>Examples using song indexes: (very quick - use indexes from slow <a href='/list-artists'>all artists command</a>)</b><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All albums by the artist of the first song in the library: <a href='/list-artist-albums%20<1>'>http://hostname:40510/list-artist-albums%20&lt;1&gt;</a><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All tracks by the first artist: <a href='/list-artist-songs%20<1>'>http://hostname:40510/list-artist-songs%20&lt;1&gt;</a><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All tracks by the first artist: <a href='/list-artist-songs%20<1>'>http://hostname:40510/list-artist-songs%20&lt;1&gt;</a><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Play the tenth song: <a href='/play-audio-song%20<10>'>http://hostname:40510/play-audio-song%20&lt;10&gt;</a><br>" +
				"<br><b>Examples using custom formats and artist match: (can be slow...)</b><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display pretty albums and tracks by the first artist starting with &quot;Jack&quot;: <a href='/list-artist-custom%20-115%20jack'>http://hostname:40510/list-artist-custom%20-115%20jack</a><br>" +
				"<br><b>More help:</b><br>" +
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Help on the music library commands: <a href='/list-artists%20-help'>http://hostname:40510/list-artists%2-help</a><br>" +
				"<br><hr>" +
				"<a id='bottom'>Generated by</a>: Vista Media Center TCP/IP Controller (<a href='http://www.codeplex.com/VmcController'>vmcController Home</a>)" +
				"<hr>Jump to: <a href='#top'>Top</a>, <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a><br>" +
				"<script LANGUAGE='JavaScript'>toggleAll(false);</script></body></html>";

			string header_start =
				"</pre></span></div><br><div id='section' onclick='toggle(this)' style='border:solid 1px black;'><font size=+1 style='font:15pt courier;'><span>-</span>";
			string header_end = "</font><span style='display:'><pre>";

			opResult.AppendFormat("{0}", page_start);

			if (port != 0) {
				opResult.AppendFormat("{0} Ports: {1}", header_start, header_end);
				opResult.AppendFormat("TCP/IP Socket port: {0}", port);
				opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10));
			}
			opResult.AppendFormat("{0} Connection Commands: {1}", header_start, header_end);
			opResult.AppendFormat("<a href='/help'>help</a> - Shows this page");
			opResult.AppendFormat("<a href='/help'>exit</a> - Closes the socket connection");
			foreach (KeyValuePair<string, ICommand> cmd in m_commands) {
				if (cmd.Key.StartsWith("===")) {
					opResult.AppendFormat(cmd.Key.Replace("==========", header_end).Replace("===", header_start));
				}
				else {
					opResult.AppendFormat("<a href='/{0}'>{1}</a> {2}",
					                      cmd.Key, cmd.Key,
					                      (cmd.Value == null) ? "" : cmd.Value.ShowSyntax().Replace("<", "&lt;").Replace(">", "&gt;"));
				}
			}
			opResult.AppendFormat("{0}", page_end);

			return opResult;
		}

		/// <summary>
		/// Executes a command with the given parameter string and returns a string return
		/// </summary>
		/// <param name="command">command name string</param>
		/// <param name="param">parameter string</param>
		/// <param name="result">string</param>
		/// <returns></returns>
		public OpResult Execute(String command, string param) {
			command = command.ToLower();
			if (m_commands.ContainsKey(command)) {
				try {
					return m_commands[command].Execute(param);
				}
				catch (Exception ex) {
					OpResult opResult = new OpResult();
					opResult.StatusCode = OpStatusCode.Exception;
					opResult.StatusText = ex.Message;
					opResult.AppendFormat(ex.Message);
					return opResult;
				}
			}
			else {
				return new OpResult(OpStatusCode.BadRequest);
			}
		}
	}
}